<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Userlogin extends CI_Controller {

	public function index()
	{
		$this->load->library('session');
		//print_r($this->session->all_userdata());
		//exit;
		if($this->session->userdata('gcwgulberg_adminid'))
        {
            redirect('../admin/index.php/dashboard');
        }
		
		$this->load->helper('form');
		$this->load->library('form_validation');
        $this->form_validation->set_rules('email','Email','requierd');
        $this->form_validation->set_rules('password','Password','requierd');
		
		
		
		if($this->form_validation->run() === FALSE)
        {
			$data['loginmsg'] = "<h3>Please enter your username and pass to login</h3>";
			$this->load->view('adminheader');
			$this->load->view('adminlogin', $data);
			$this->load->view('adminfooter');
		}
		else{
			$userdata['siteuser_loginid']     = $this->input->post('siteusername');
            $userdata['siteuser_password'] = $this->input->post('siteuserpass');
			$userdata['siteuser_type'] = '1';
			$userdata['siteuser_status'] = '1';
            
			$sql = "SELECT * from tbl_login WHERE login_id=? AND login_pass=? AND login_type=? AND login_status=?";
			
            $query = $this->db->query($sql, array($userdata['siteuser_loginid'],$userdata['siteuser_password'], $userdata['siteuser_type'], $userdata['siteuser_status']));
            
            if($query->num_rows() > 0 ){
				
				$data = $query->result_array();
				$row = $data[0];
				$sessions = array('gcwgulberg_adminid' => $row['id'] ,'siteuser_loginid' => $row['login_id'] , 'siteuser_name' => 'Administrator');
				
				$this->session->set_userdata($sessions);
				redirect('../admin/index.php/dashboard');
                /*foreach($query->result_array() as $row){
                        $sessions = array('gcwgulberg_adminid' => $row['user_id'] ,'siteuser_loginid' => $row['login_id'] , 'siteuser_name' => 'Administrator');
						print_r($sessions);
                        $this->session->set_userdata($sessions);
                        redirect('../admin/index.php/dashboard');
                }*/
                
            }else{
				$data['loginmsg'] = '<h3 class="error">The username and pass you entered is wrong!</h3>';
                $this->load->view('adminheader');
				$this->load->view('adminlogin', $data);
				$this->load->view('adminfooter');
            }
		}
	}
	
	public function userlogout(){
		$this->session->sess_destroy();
		redirect('../admin/index.php/userlogin');
		
	}
	
}

